package com.wsoft.hr.pm.manager;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.HrPmRedisKeyConstant;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.entity.BaseEntity;
import com.wsoft.hr.pm.entity.HrPmGradeOrgEntity;
import com.wsoft.hr.pm.mapper.HrPmGradeOrgMapper;
import com.wsoft.hr.pm.query.grade.org.HrPmGradeOrgListQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Z001
 * @Date 2025-03-01 08:35:23
 */
@Service
public class HrPmGradeOrgManager extends ServiceImpl<HrPmGradeOrgMapper, HrPmGradeOrgEntity> {

    @Resource
    private RedisService redisService;

    /**
     * 根据组织id和评级id查询
     * @param gradeId
     * @param hrOrgId
     * @return
     */
    public HrPmGradeOrgEntity getByOrgAndGradeId(Long gradeId){
        if(null == gradeId){
            return null;
        }
        QueryWrapper<HrPmGradeOrgEntity> query = new QueryWrapper<>();
        //搜索参数
        query.lambda().eq(HrPmGradeOrgEntity::getGradeId,gradeId);
        return super.getOne(query);
    }

    /**
     * 根据business获取
     * @param businessCode
     * @return
     */
    public HrPmGradeOrgEntity getByBusinessCode(String businessCode){
        QueryWrapper<HrPmGradeOrgEntity> query = new QueryWrapper<>();
        //搜索参数
        query.lambda().eq(HrPmGradeOrgEntity::getBusinessCode,businessCode);
        return super.getOne(query);
    }

    /**
     * 根据组织id 集合和评级id查询
     * @param gradeId
     * @param hrOrgIds
     * @return
     */
    public List<HrPmGradeOrgEntity> getByOrgAndGradeIds(Long gradeId,List<Long> hrOrgIds){
        if(null == gradeId || CollUtil.isEmpty(hrOrgIds)){
            return null;
        }
        QueryWrapper<HrPmGradeOrgEntity> query = new QueryWrapper<>();
        //搜索参数
        query.lambda().in(HrPmGradeOrgEntity::getHrOrgId,hrOrgIds)
                .eq(HrPmGradeOrgEntity::getGradeId,gradeId);
        return super.list(query);
    }

    /**
     * 查询列表
     * @param param
     * @return
     */
    public List<HrPmGradeOrgEntity> list(HrPmGradeOrgListQuery param){
        QueryWrapper<HrPmGradeOrgEntity> query = new QueryWrapper<>();
        //搜索参数
        query.lambda().eq(null != param.getGradeId(),HrPmGradeOrgEntity::getGradeId,param.getGradeId())
                .in(CollUtil.isNotEmpty(param.getGradeIdList()),HrPmGradeOrgEntity::getGradeId,param.getGradeIdList())
                .orderByDesc(BaseEntity::getCreateTime);
        return super.list(query);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetRedisCache(type = HrPmRedisKeyConstant.GRADE_ORG_INFO,
            expire_sec = HrPmRedisKeyConstant.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public HrPmGradeOrgEntity getById(Long id){
        return super.getById(id);
    }

    /**
     * 新增
     * @param entity
     * @return
     */
    @Override
    public boolean save(HrPmGradeOrgEntity entity){
        return super.save(entity);
    }

    /**
     * 更新
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(HrPmGradeOrgEntity entity){
        boolean b = super.updateById(entity);
        if(b){
            redisService.del(HrPmRedisKeyConstant.GRADE_ORG_INFO + entity.getId());
        }
        return b;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        boolean b = super.removeById(id);
        if(b){
            redisService.del(HrPmRedisKeyConstant.GRADE_ORG_INFO + id);
        }
        return b;
    }

    /**
     * 根据评级id删除
     * @param gradeId
     * @return
     */
    public boolean removeByGradeId(Long gradeId,List<Long> hrOrgIdList){
        if(null == gradeId){
            return false;
        }
        QueryWrapper<HrPmGradeOrgEntity> query = new QueryWrapper<>();
        query.lambda().eq(HrPmGradeOrgEntity::getGradeId,gradeId)
                .in(HrPmGradeOrgEntity::getHrOrgId,hrOrgIdList);
        boolean b = super.remove(query);
        if(b){
            redisService.deleteByKeyPattern(HrPmRedisKeyConstant.GRADE_ORG_INFO + RedisKeyConstant.PATTERN_SYMBOL);
        }
        return b;
    }
}
